package tests;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;

import junit.framework.Assert;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.apache.log4j.Logger;

import pages.GoogleSearchPage;
import pages.GoogleSearchResultsPage;
import common.ReadTestDataXML;

@RunWith(Parameterized.class)
public class SearchPageTestCases {
	
	static String CHROME_PATH = "C:\\Users\\Preeti Jain\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
	public String BROWSER;
	static Logger logApps = Logger.getLogger("SearchPageTestCases");
	
	@Rule
	public ErrorCollector errCollector = new ErrorCollector();
	
	public SearchPageTestCases(String browser) {
		this.BROWSER = browser;
	}
	
	@Test
	public void testSearchPage() {
		System.out.println("in testSearchPage");
		logApps.debug("in testSearchPage");
		
		//WebDriver driver = new HtmlUnitDriver();
		GoogleSearchPage cGoogleSearchPage = new GoogleSearchPage();
		GoogleSearchResultsPage cGoogleSearchResultsPage = new GoogleSearchResultsPage();
		
		String testcaseFileName = getTestCaseFileName();
		ReadTestDataXML readDataXML = new ReadTestDataXML();
		HashMap<String, String> initialData = readDataXML.parseTestDataXML(testcaseFileName, "InitialSetup");
		
		StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
		StackTraceElement es = stacktrace[1];//coz 0th will be getStackTrace so 1st
		String methodName = es.getMethodName();
		System.out.println("Methodname:" + methodName);
		    
		HashMap<String, String> testData = readDataXML.parseTestDataXML(testcaseFileName, methodName);
		System.out.println("textToSearch = " + testData.get("textToSearch"));

		
		// About page test case
		WebDriver driver = getBrowser(BROWSER);
		//WebDriver driver = new FirefoxDriver(); // new HtmlUnitDriver();
		driver.get("http://www.google.com");
		//WebElement element = driver.findElement(By.name("q"));
		WebElement element = driver.findElement(By.name(cGoogleSearchPage.getGoogleSearch()));

		//element.sendKeys("Potato!");
		element.sendKeys(testData.get("textToSearch"));
		
		element.click();
		element.submit();
		
		System.out.println("Page title is: " + driver.getTitle());
		System.out.println("Current page url is " + driver.getCurrentUrl());
		System.out.println("getResultsStats = " + cGoogleSearchResultsPage.getResultStats());

		try {
		Thread.sleep(1000);
		} catch(InterruptedException e) {
			
		}
		
		int err = 0;
		try {
			WebElement ele1 = driver.findElement(By.id(cGoogleSearchResultsPage.getOptionsButtonIcon()));
			Assert.assertEquals(true, ele1.isDisplayed());
		} catch(Throwable t) {
				err++;
				System.out.println("ERROR ENCOUNTERED");
				logApps.debug("ERROR ENCOUNTERED");
				//javacode report error
				errCollector.addError(t);
		}
		if (err == 0) {
			System.out.println("Success!!!");
			logApps.debug("Success!!!");
		}
		driver.quit();

	}
	@Test
	public void testSearchPage2() {
		System.out.println("in testSearchPage2");
		logApps.debug("in testSearchPage2");
		
		//WebDriver driver = new HtmlUnitDriver();
		GoogleSearchPage cGoogleSearchPage = new GoogleSearchPage();
		GoogleSearchResultsPage cGoogleSearchResultsPage = new GoogleSearchResultsPage();
		
		String testcaseFileName = getTestCaseFileName();
		ReadTestDataXML readDataXML = new ReadTestDataXML();
		HashMap<String, String> initialData = readDataXML.parseTestDataXML(testcaseFileName, "InitialSetup");
		
		//Get current testcase/method name
		StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
		StackTraceElement es = stacktrace[1];//coz 0th will be getStackTrace so 1st
		String methodName = es.getMethodName();
		//System.out.println("Methodname:" + methodName);
		    
		HashMap<String, String> testData = readDataXML.parseTestDataXML(testcaseFileName, methodName);
		System.out.println("textToSearch = " + testData.get("textToSearch"));

		
		// About page test case
		
		WebDriver driver = new FirefoxDriver(); // new HtmlUnitDriver();
		driver.get("http://www.google.com");
		//WebElement element = driver.findElement(By.name("q"));
		WebElement element = driver.findElement(By.name(cGoogleSearchPage.getGoogleSearch()));

		//element.sendKeys("Potato!");
		element.sendKeys(testData.get("textToSearch"));
		
		element.click();
		element.submit();
		
		System.out.println("Page title is: " + driver.getTitle());
		System.out.println("Current page url is " + driver.getCurrentUrl());
		System.out.println("getResultsStats = " + cGoogleSearchResultsPage.getResultStats());

		try {
		Thread.sleep(1000);
		} catch(InterruptedException e) {
			
		}
		
		int err = 0;
		try {
			WebElement ele1 = driver.findElement(By.id(cGoogleSearchResultsPage.getOptionsButtonIcon()));
			Assert.assertEquals(true, ele1.isDisplayed());
		} catch(Throwable t) {
				err++;
				System.out.println("ERROR ENCOUNTERED");
				logApps.debug("ERROR ENCOUNTERED");
				//javacode report error
				errCollector.addError(t);
		}
		if (err == 0) {
			System.out.println("Success!!!");
			logApps.debug("Success!!!");
		}
		driver.quit();

	}

	public String getTestCaseFileName(){
		String testcaseFileName = this.getClass().getSimpleName();
		System.out.println("Calling class name is " + testcaseFileName);
		
		//form the xml file path
		File directory = new File (".");
		String absPath = null;
		try {
			System.out.println ("Current directory's absolute  path: " 
					+ directory.getAbsolutePath());
			absPath = new String(directory.getAbsolutePath());
		}catch(Exception e) {
			 	System.out.println("Exceptione is ="+e.getMessage());
		}
         
		String temp [] = null;
		temp = absPath.split("\\.");
		String xmlPath = temp[0] + "src\\testdata\\" + testcaseFileName + ".xml";
		System.out.println("Xml file path is => " + xmlPath);
		return xmlPath;
	}
	WebDriver getBrowser(String browser){
		
		WebDriver driver = null;
		System.out.println("Browser: " + browser);

		  if (browser.equals("FF")) {
		   System.out.println("FF is selected");
		   driver = new FirefoxDriver();
		  } else if (browser.equals("IE")) {
		   System.out.println("IE is selected");
		   driver = new InternetExplorerDriver();
		  } else if (browser.equals("HU")) {
		   System.out.println("HU is selected");
		   driver = new HtmlUnitDriver();
		  } else if (browser.equals("CH")){
		   System.out.println("Google chrome is selected");
		   System.setProperty("webdriver.chrome.driver", "c:\\selenium\\chromedriver.exe");
		   driver = new ChromeDriver();
		  }
		  return driver;
	}
	@Parameters
	public static Collection<Object[]> getData(){
		//rows = number of times you want to repeat the test
		//col = number of parameters you want to pass
		
		Object[][] data = new Object[3][1];
		
		data[0][0] = "FF";
		data[1][0] = "IE";
		data[2][0] = "CH";
		//data[3][0] = "HU";
		
		return Arrays.asList(data);
	}
}
